// src/router/index.ts
import { createRouter, createWebHistory } from "vue-router";
import { ElMessage } from "element-plus";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: () => import("../views/Login.vue"),
    },

    {
      path: "/menu",
      name: "menu",
      component: () => import("../views/MenuView.vue"),
      meta: { requiresAuth: true }, // 登录保护
      children: [
        //概况
        {
          path: "/system/Main/MainView",
          name: "MainView",
          component: () => import("../views/Main/MainView.vue"),
        },
        //房态
        {
          path: "/RoomStatus/RoomStatus",
          name: "RoomStatus",
          component: () => import("../views/RoomStatus/RoomStatusView.vue"),
        },
        //房态预约
        {
          path: "/RoomStatus/RoomReservation",
          name: "RoomReservation",
          component: () =>
            import("../views/RoomStatus/RoomReservationView.vue"),
        },
        //订单
        {
          //订单菜单
          path: "/Order/OrderView",
          name: "Order",
          component: () => import("../views/Order/OrderView.vue"),
          children: [
            //住宿订单-订房订单
            {
              path: "/Order/AccommodationOrders/BookingOrder",
              name: "BookingOrder",
              component: () =>
                import(
                  "../views/Order/AccommodationOrders/BookingOrderView.vue"
                ),
            },
            //住宿订单-住宿记录
            {
              path: "/Order/AccommodationOrders/AccommodationRecords",
              name: "AccommodationRecords",
              component: () =>
                import(
                  "../views/Order/AccommodationOrders/AccommodationRecordsView.vue"
                ),
            },
            //商城订单-订单记录
            {
              path: "/Order/MallOrders/MallOrderRecords",
              name: "MallOrderRecords",
              component: () =>
                import("../views/Order/MallOrders/MallOrderRecordsView.vue"),
            }, //商城订单-预约订单
            {
              path: "/Order/MallOrders/ReservationOrder",
              name: "ReservationOrder",
              component: () =>
                import("../views/Order/MallOrders/ReservationOrderView.vue"),
            }, //商城订单-核销记录
            {
              path: "/Order/MallOrders/VerificationRecord",
              name: "VerificationRecord",
              component: () =>
                import("../views/Order/MallOrders/VerificationRecordView.vue"),
            },
          ],
        },
        //业务
        {
          path: "/Business",
          name: "Business",
          component: () => import("../views/Business/BusinessView.vue"),
          children: [
            //商品列表
            {
              path: "/Commodity",
              name: "Commodity",
              component: () =>
                import("../views/Business/CommodityManage/CommodityView.vue"),
            },
            //新增商品
            {
              path: "/InsertCommodity",
              name: "InsertCommodity",
              component: () =>
                import(
                  "../views/Business/CommodityManage/InsertCommodityView.vue"
                ),
            },
            //预约列表
            {
              path: "/Reservation",
              name: "Reservation",
              component: () =>
                import("../views/Business/CommodityManage/ReservationView.vue"),
            },
            //添加预约
            {
              path: "/InsertReservation",
              name: "InsertReservation",
              component: () =>
                import(
                  "../views/Business/CommodityManage/InsertReservationView.vue"
                ),
            },
            //修改商品
            {
              path: "/UpdateCommodity",
              name: "UpdateCommodity",
              component: () =>
                import(
                  "../views/Business/CommodityManage/UpdateCommodityView.vue"
                ),
            },
            //修改商品
            {
              path: "/UpdateReservation",
              name: "UpdateReservation",
              component: () =>
                import(
                  "../views/Business/CommodityManage/UpdateReservationView.vue"
                ),
            },
            //商品标签列表
            {
              path: "/ProductLabel",
              name: "ProductLabel",
              component: () =>
                import(
                  "../views/Business/CommodityManage/ProductLabelView.vue"
                ),
            },
            //商品标签列表
            {
              path: "/Sort",
              name: "Sort",
              component: () =>
                import("../views/Business/CommodityManage/SortView.vue"),
            },
            //住宿设置
            {
              path: "/Stay",
              name: "Stay",
              component: () => import("../views/Business/StayView.vue"),
            },
            //房型设置
            {
              path: "/RoomType",
              name: "RoomType",
              component: () =>
                import("../views/Business/RoomTypeManage/RoomTypeView.vue"),
            },
            //房价管理
            {
              path: "/RoomMoney",
              name: "RoomMoney",
              component: () =>
                import("../views/Business/RoomRates/RoomMoneyView.vue"),
            },
            //房号管理
            {
              path: "/RoomNum",
              name: "RoomNum",
              component: () =>
                import("../views/Business/RoomTypeManage/RoomNumView.vue"),
            },
            //房号管理
            {
              path: "/RoomStatePreserve",
              name: "RoomStatePreserve",
              component: () =>
                import("../views/Business/RoomRates/RoomStatePreserveView.vue"),
            },
            //菜品管理
            {
              path: "/DishesView",
              name: "DishesView",
              component: () => import("../views/Business/Menu/DishesView.vue"),
            },
            {
              path: "/InsertDishesView",
              name: "InsertDishesView",
              component: () =>
                import("../views/Business/Menu/InsertDishesView.vue"),
            },
            {
              path: "/UpdateDishesView",
              name: "UpdateDishesView",
              component: () =>
                import("../views/Business/Menu/UpdateDishesView.vue"),
            },
          ],
        },
        //用户中心
        {
          path: "/RBAC/RBACView",
          name: "RBAC",
          component: () => import("../views/RBAC/RBACView.vue"),
          children: [
            //用户管理
            {
              path: "/RBAC/RBACManage/User",
              name: "User",
              component: () => import("../views/RBAC/RBACManage/UserView.vue"),
            },
            //角色管理
            {
              path: "/RBAC/RBACManage/RoleView",
              name: "Role",
              component: () => import("../views/RBAC/RBACManage/RoleView.vue"),
            },
            //权限管理
            {
              path: "/RBAC/RBACManage/Permissions",
              name: "Permissions",
              component: () =>
                import("../views/RBAC/RBACManage/Permissions.vue"),
            },
          ],
        },
        //客户
        {
          path: "/Custorm",
          name: "Custorm",
          component: () => import("../views/Custorm/CustormView.vue"),
          children: [
            {
              path: "/Custorm/Custorms/CustormsList",
              name: "CustormsList",
              component: () =>
                import("../views/Custorm/Custorms/CustormsListView.vue"),
            },
            {
              path: "/Custorm/Custorms/CustormsQuery",
              name: "CustormsQuery",
              component: () =>
                import("../views/Custorm/Custorms/CustormsQueryView.vue"),
            },
            {
              path: "/Custorm/VIP/VipManagement",
              name: "VipManagement",
              component: () =>
                import("../views/Custorm/VIP/VipManagementView.vue"),
            },
            {
              path: "/Custorm/VIP/InsertVipManagementView",
              name: "InsertVipManagementView",
              component: () =>
                import("../views/Custorm/VIP/InsertVipManagementView.vue"),
            },
            {
              path: "/Custorm/VIP/MeetingEquity",
              name: "MeetingEquity",
              component: () =>
                import("../views/Custorm/VIP/MeetingEquityView.vue"),
            },
            {
              path: "/Custorm/AssetManage/StoredSetting",
              name: "StoredSetting",
              component: () =>
                import("../views/Custorm/AssetManage/StoredSettingView.vue"),
            },
            {
              path: "/Custorm/AssetManage/ScoreSetting",
              name: "ScoreSetting",
              component: () =>
                import("../views/Custorm/AssetManage/ScoreSettingView.vue"),
            },
            {
              path: "/Custorm/AssetLog/BalanceStatement",
              name: "BalanceStatement",
              component: () =>
                import("../views/Custorm/AssetLog/BalanceStatementView.vue"),
            },
            {
              path: "/Custorm/AssetLog/ScoreLog",
              name: "ScoreLog",
              component: () =>
                import("../views/Custorm/AssetLog/ScoreLogView.vue"),
            },
          ],
        },
      ],
    },
    {
      path: "/404",
      name: "404",
      component: () => import("../views/Error/404.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token"); // 获取存储的token
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 检查目标路由是否需要登录
    if (!token) {
      ElMessage({
        message: "您尚未登录，请登录后再访问！",
        type: "warning",
      });
      next({
        path: "/", // 如果没有token，则重定向到登录页面
        query: { redirect: to.fullPath }, // 保存当前路径，以便登录后可以重定向回来
      });
    } else {
      next(); // 如果有token，则允许进入路由
    }
  } else {
    next(); // 如果目标路由不需要登录，则直接通过
  }
});

export default router;
